let dId= 0

export class Dep {
    constructor() {
        this.id = dId++
        this.subs = []

    }

    depend() {
        if(Dep.target) {
            Dep.target.addDep(this)
        }
    }

    notify () {
        const tempSubs = this.subs.slice()
        tempSubs.reverse().forEach(watcher => watcher.update())
    }

    addSub(watcher) {
        this.subs.push(watcher) // 将atcher收入到subs
    }
}


let stack = []

export function pushTarget(watcher) {
    Dep.target = watcher
    stack.push(watcher)
}

export function poptarget() {
    stack.pop()
    Dep.target = stack[stack.length - 1]
}


